
#include "reverse_iterator.h"

namespace my_vector
{
    template<class T>
    class vector 
    {
    public:
        typedef T* iterator;
        typedef const T* const_iterator;
        typedef my_riterator::__reverse_iterator<iterator, T&, T*> reverse_iterator;
        typedef my_riterator::__reverse_iterator<const_iterator, const T&, const T*> const_reverse_iterator;

        reverse_iterator rbegin() { return reverse_iterator(end()); }
        const_reverse_iterator rbegin() const { return const_reverse_iterator(end()); }
        reverse_iterator rend() { return reverse_iterator(begin()); }
        const_reverse_iterator rend() const { return const_reverse_iterator(begin()); }

        iterator begin() { return _start; }
        const_iterator begin() const { return _start; }
        iterator end() { return _finish; }
        const_iterator end() const { return _finish; }

        void swap(vector<T>& v)
        {
            ::swap(_start, v._start);
            ::swap(_finish, v._finish);
            ::swap(_end_of_storagr, v._end_of_storagr);
        }

        vector()
            :_start(nullptr)
             ,_finish(nullptr)
             ,_end_of_storagr(nullptr)
        {}

        template<class InputIterator>
        vector(InputIterator first, InputIterator last)
            :_start(nullptr)
             ,_finish(nullptr)
             ,_end_of_storagr(nullptr)
        {
            while (first != last)
            {
                push_back(*first);
                ++ first;
            }
        }

        vector(const vector<T>& v)
            :_start(nullptr)
             ,_finish(nullptr)
             ,_end_of_storagr(nullptr)
        {
            vector<T> tmp(v.begin(), v.end());
            swap(tmp);
        }

        vector(size_t n, const T& x)
            :_start(nullptr)
             ,_finish(nullptr)
             ,_end_of_storagr(nullptr)
        {
            reserve(n);

            for (size_t i = 0; i < n; i ++ )
            {
                push_back(x);
            }
        }
        vector(int n, const T& x)
            :_start(nullptr)
             ,_finish(nullptr)
             ,_end_of_storagr(nullptr)
        {
            for (int i = 0; i < n; i ++ )
            {
                push_back(x);
            }
        }

        ~vector()
        {
            delete[] _start;
            _start = _finish = _end_of_storagr = nullptr;
        }

        vector<T>& operator=(vector<T> v)
        {
            swap(v);

            return *this;
        }

        size_t size() const { return _finish - _start; }
        size_t capacity() const { return _end_of_storagr - _start; }
        bool empty() const { return _start == _finish; }

        void reserve(size_t n)
        {
            if (n > capacity())
            {
                iterator tmp = new T[n];
                size_t sz = size();

                if (_start)
                {
                    for (size_t i = 0; i < size(); i ++ )
                    {
                        tmp[i] = _start[i];
                    }
                    delete[] _start;
                }

                _start = tmp;
                _finish  = _start + sz;
                _end_of_storagr = _start + n;
            }
        }

        void resize(size_t n, const T& x = T())
        {
            if (n > capacity())
            {
                reserve(n);
            }

            if (n > size())
            {
                for (size_t i = size(); i < n; i ++ )
                {
                    push_back(x);
                }
            }
            else 
            {
                _finish = _start + n;
            }
        }

        void push_back(const T& x)
        {
            if (_finish == _end_of_storagr)
            {
                reserve(capacity() == 0 ? 4 : capacity() * 2);
            }

            *_finish = x;
            ++ _finish;
        }

        void pop_back()
        {
            assert(!empty());

            -- _finish;
        }

        T& back()
        {
            assert(!empty());

            return *(_finish - 1);
        }
        const T& back() const 
        {
            assert(!empty());

            return *(_finish - 1);
        }
        T& front()
        {
            assert(!empty());

            return *_start;
        }
        const T& front() const 
        {
            assert(!empty());

            return *_start;
        }

        T& operator[](size_t pos)
        {
            assert(pos < size());

            return _start[pos];
        }
        const T& operator[](size_t pos) const 
        {
            assert(pos < size());

            return _start[pos];
        }

        iterator insert(iterator pos, const T& x)
        {
            assert(pos >= _start && pos <= _finish);
            
            size_t sz = pos - _start;
            if (_finish == _end_of_storagr)
            {
                reserve(capacity() == 0 ? 4 : capacity() * 2);
            }
            pos = _start + sz;

            iterator end = _finish;
            while (end > pos)
            {
                *end = *(end - 1);
                -- end;
            }

            *pos = x;
            ++ _finish;

            return pos;
        }
        iterator erase(iterator pos)
        {
            assert(!empty() && pos >= _start && pos <= _finish);

            iterator begin = pos;
            while (begin < _finish - 1)
            {
                *begin = *(begin + 1);
                ++ begin;
            }

            -- _finish;

            return pos;
        }

        void clear()
        {
            _finish = _start;
        }

    private:
        iterator _start;
        iterator _finish;
        iterator _end_of_storagr;
    };
}

